using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.DataTier.Source;

namespace OR.Lazarus.Library.DataTier.Source.Lazarus
{
	/// <summary>
	/// Defines the objects to be affected by a CREATE, UPDATE or CUSTOM method.
	/// </summary>
   public class EffectObjectRefDetail : EntityBase
   {
		protected System.String mName;
		protected string mEffectType;
		protected string mRef;
		protected string mDescription;
	
		protected Derived.FieldCollection mFields;
		protected Derived.FilterCollection mFilters;

		public EffectObjectRefDetail()
		{
			mFields = new Derived.FieldCollection();
			mFields.ParentEntity = this;
			mFilters = new Derived.FilterCollection();
			mFilters.ParentEntity = this;
	
		}
	
		/// <summary>
		/// Specifies a unique name for the entity.
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// Defines the type of effect being performed on the object.  For CUSTOM methods, this may be 'Create' or 'Update'.
		/// </summary>
		public string EffectType
		{
			get
			{
				return ( mEffectType );
			}
			set
			{
				mEffectType = value;
			}
		}

		/// <summary>
		/// Defines the object that will be affected by the Create, Update or Custom method.
		/// </summary>
		public string Ref
		{
			get
			{
				return ( mRef );
			}
			set
			{
				mRef = value;
			}
		}

		/// <summary>
		/// Provides a long-text description for the entity.
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.FieldCollection Fields
		{
			get
			{
				return ( mFields );
			}
			set
			{
				mFields = value;
			}
		}

		public Derived.FilterCollection Filters
		{
			get
			{
				return ( mFilters );
			}
			set
			{
				mFilters = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mEffectType = DataUtility.GetString( pSource, "effecttype" );
			mRef = DataUtility.GetString( pSource, "ref" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lz:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
			XmlElement vFields = ( XmlElement )pSource.SelectSingleNode( "lz:fields", PersistenceConfig.Namespaces );
			
			if ( vFields != null )
			{
				mFields.LoadXml( vFields );
			}
			XmlElement vFilters = ( XmlElement )pSource.SelectSingleNode( "lz:method-filters", PersistenceConfig.Namespaces );
			
			if ( vFilters != null )
			{
				mFilters.LoadXml( vFilters );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lz", "object-ref", "http://schemas.icitadel.com/Lazarus/Source.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mEffectType ) )
			{
				vNewElement.SetAttribute( "effecttype", mEffectType );
			}
					
			if ( !String.IsNullOrEmpty( mRef ) )
			{
				vNewElement.SetAttribute( "ref", mRef );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lz", "description", "http://schemas.icitadel.com/Lazarus/Source.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mFields.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mFilters.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.EffectObjectRefDetail v_oReturn = new Derived.EffectObjectRefDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.EffectType = mEffectType;
			v_oReturn.Ref = mRef;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections

			foreach ( Derived.FieldDetail vDetail in mFields )
			{
				v_oReturn.Fields.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.FilterDetail vDetail in mFilters )
			{
				v_oReturn.Filters.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.EffectObjectRefDetail CloneTyped()
		{
			return ( ( Derived.EffectObjectRefDetail )this.Clone() );
		}
   }
}
   